﻿using Ndns.ServerHost.Abstractions;
using System.Collections.Generic;
using Ndns.ServerHost.Abstractions.Features;
using System.Threading;

namespace Ndns.ServerHost
{
    class DnsContextImpl : DnsContext
    {
        internal DnsContextImpl(
            string traceIdentifier,
            ConnectionInfo connection,
            IFeatureCollection features,
            DnsRequest request,
            DnsResponse response,
            CancellationToken requestAborted
            )
        {
            TraceIdentifier = traceIdentifier;
            Items = new Dictionary<object, object>();
            Connection = connection;
            Features = features;
            Request = request;
            Response = response;
            _abortCts = CancellationTokenSource.CreateLinkedTokenSource(requestAborted);
            RequestAborted = _abortCts.Token;
        }

        private readonly CancellationTokenSource _abortCts;

        public override string TraceIdentifier { get; set; }
        public override IDictionary<object, object> Items { get; set; }

        public override ConnectionInfo Connection { get; }

        public override IFeatureCollection Features { get; }

        public override DnsRequest Request { get; }

        public override DnsResponse Response { get; }

        public override CancellationToken RequestAborted { get; set; }

        public override void Abort()
        {
            _abortCts.Cancel();
        }
    }
}
